Repository navigation
feat(platform): add MCP Tasks extension support to McpService - #2015
Open
alessandromuresan wants to merge 5 commits into
Open
alessandromuresan wants to merge 5 commits into
alessandromuresan wants to merge 5 commits into
Conversation
A long-running tools/call on a UiPath MCP server can now return a durable task handle instead of holding the request open. McpService gains call_tool / get_task / cancel_task / wait_for_task / call_tool_and_wait (sync + async): they declare the io.modelcontextprotocol/tasks extension per request over the stateless 2026-07-28 protocol revision - the only revision the extension is served on - and poll tasks/get at the server-suggested cadence until terminal. call_tool_and_wait makes an hour-long process invocation look synchronous to coded agents, and gives the langchain runtime one shared implementation to wire its MCP tool layer to. The official mcp SDK cannot express this wire shape (1.x models the older 2025-11-25 task flavor, 2.x dropped tasks), so the handful of shapes are local pydantic models.
Contributor
There was a problem hiding this comment.
🟡 Changes recommended
Both synchronous and asynchronous polling can exceed the caller’s timeout, and SSE parsing can discard valid results when notifications precede the response.
3 open issues
What changed in this PR
Adds MCP Tasks extension support to the UiPath Platform SDK, allowing McpService to invoke long-running tools and poll durable task handles for their results.
Changes:
- Adds sync and async APIs for tool calls, task polling, waiting, and cancellation.
- Introduces public task models and errors, plus protocol metadata, routing headers, and JSON/SSE response parsing.
- Adds mocked HTTP tests for synchronous results, task lifecycles, error handling, and async polling.
| File | Description |
|---|---|
packages/uipath-platform/tests/services/test_mcp_tasks.py |
Tests task workflows, request formatting, and response handling. |
packages/uipath-platform/src/uipath/platform/orchestrator/mcp_tasks.py |
Defines task states, handles, outcomes, and exceptions. |
packages/uipath-platform/src/uipath/platform/orchestrator/_mcp_service.py |
Implements task-capable tool calls and lifecycle operations. |
packages/uipath-platform/src/uipath/platform/orchestrator/__init__.py |
Exports the new task models and errors. |
Review effort: Balanced
Comment on lines
+437
to
+438
| time.sleep(self._poll_delay_seconds(snapshot, deadline, timeout)) | ||
| snapshot = self.get_task(snapshot) |
Comment on lines
+463
to
+472
| deadline = None if timeout is None else time.monotonic() + timeout | ||
| snapshot = task | ||
|
|
||
| while True: | ||
| terminal = self._terminal_result(snapshot) | ||
| if terminal is not None: | ||
| return terminal | ||
|
|
||
| await asyncio.sleep(self._poll_delay_seconds(snapshot, deadline, timeout)) | ||
| snapshot = await self.get_task_async(snapshot) |
Comment on lines
+666
to
+681
| text = response.text | ||
| if "text/event-stream" in response.headers.get("content-type", ""): | ||
| data_lines: list[str] = [] | ||
| for line in text.replace("\r\n", "\n").split("\n"): | ||
| if not line and data_lines: | ||
| break | ||
| if line.startswith("data:"): | ||
| data_lines.append(line[5:].lstrip()) | ||
| if not data_lines: | ||
| raise McpToolCallError( | ||
| 0, f"The MCP SSE response to '{method}' had no data event." | ||
| ) | ||
| text = "\n".join(data_lines) | ||
|
|
||
| try: | ||
| envelope = json.loads(text) |
Three fixes from the Copilot review of #2015: - SSE framing: _parse_mcp_envelope stopped at the first data event, so a stream that delivers a notification before the response read the notification as the answer and reported a perfectly good result as missing. Every event is now parsed and the first one carrying result or error wins; a body with no response at all says so explicitly. - The inter-poll sleep was clamped to the deadline but the poll that followed was unbounded, so a slow or stalled tasks/get could answer long after the caller's timeout. The remaining budget is now re-checked after the sleep and passed as the request timeout, in both the sync and async waiters, with a transport timeout surfacing as TimeoutError naming the task. - Both waiters raise the same TimeoutError shape, so a caller can still poll or cancel the task it names. Jira: PRODEV-2011 Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Lifts new-code coverage over the Sonar gate (89.6% -> both new files at 100%): the async twins of get/cancel, both synchronous-passthrough shortcuts, the cancelled terminal state, a server with no MCP URL, the poll-bound timeout on each loop, and the SSE/envelope framing edges only a malformed server produces. Also bumps uipath-platform to 0.3.15 (0.3.14 is on PyPI) and records the one thing the per-poll request bound does not do: the platform retry policy treats a timeout as transient and re-sends, so an unreachable server can still overrun the caller's timeout by the retry budget before the loop gives up. What it does prevent is a *successful* snapshot arriving past the deadline and being taken as the result. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
|
Addresses codex P1 on #2015. Bounding each tasks/get by the remaining budget was not enough: the platform retry policy treats a timeout as transient and re-sends it up to MAX_RETRY_ATTEMPTS times with exponential backoff between them, so a wait could return long after its documented timeout -- and in a serverless runtime, eat the execution window it was sized against. Polls now go out as a single attempt (`_request_once`, tenacity's own `retry_with`), and the poll loop is the retry: it re-sends on the task's own cadence and re-checks the deadline every time, so a transient blip still does not end a long wait. A poll bounded by the remaining budget timing out *is* the deadline arriving, so it becomes the task timeout. Waits with no timeout have no deadline to bound them, so five consecutive failures end them instead of polling a vanished server forever. Tests pin all four: one request per timed-out poll (not five), a blip absorbed by the loop in both the sync and async paths, the failure cap, and a non-transient status raised at once. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Review finding: tools/call went out through the service-wide retry policy, which re-sends on a read timeout and on 408/429/502/503/504/524, up to five attempts. The httpx client times out at 30s and the server decides whether to answer synchronously, so an ordinary slow tool -- or one gateway 504 -- could submit the same call five times and start five Orchestrator jobs. Reproduced: four failures ahead of a success produced five POSTs. A read timeout means the server received the request; only a failed connect proves it did not. Submission now uses `resend="connect-only"`, which retries ConnectError and nothing else, while polls keep `never` (the loop retries) and idempotent calls keep the platform policy. This is the submit-side counterpart of the resubmission guard already added to the poll path. Also: a task parked on `input_required` used to be polled forever -- it is not terminal per spec and `timeout` defaults to None -- and now raises `McpTaskInputRequiredError`, matching the langchain client and carrying the handle for a caller that can answer. `clientInfo.version` reports the real package version instead of "1.0.0", and `McpToolCallOutcome` enforces the either/or its docstring promises. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.




What
Adds MCP Tasks extension support (ext-tasks revision 2026-07-28) to
McpService, so the SDK can call an MCP tool whose work outlives the HTTP request — in particular a UiPath-type MCP server tool that starts an Orchestrator job.A task-capable
tools/calldeclares the extension per request; the server decides whether to answer synchronously or with a durable task handle, and the caller pollstasks/getuntil the task is terminal.API
Each method has a sync and an
_asyncform:call_toolMcpToolCallOutcome(resultortask)get_taskwait_for_taskpollIntervalMs, with an optional timeoutcall_tool_and_waitCallToolResultcancel_taskModels live in
orchestrator/mcp_tasks.py:McpTaskStatus,McpTask,McpToolCallOutcome, and the errorsMcpToolCallError/McpTaskFailedError/McpTaskCancelledError.Notes for reviewers
_mcp_rpc_envelopesends the per-request_meta["io.modelcontextprotocol/clientCapabilities"]declaration plus theMCP-Protocol-Version/Mcp-Method/Mcp-Nameheaders;_parse_mcp_envelopeaccepts both a JSON body and an SSE frame.McpTask.server_urlis client-only state (which endpoint minted the handle) and is excluded from the wire, so a task can be polled without re-resolving the server.ttlMs/pollIntervalMs, flatCreateTaskResultkeyed byresultType: "task"). The SDK's ownmcp.types.Taskstill carries the superseded SEP-1686 names (ttl/pollInterval) and cannot validate this wire — see typescript-sdk#2782, which lands the same flat shape.cancel_taskif that is not wanted.Tests
packages/uipath-platform/tests/services/test_mcp_tasks.py— 10 pytest-httpx tests covering the synchronous answer, the task answer, polling to completion, failed/cancelled terminal states, timeout, and cancellation. All 34 mcp-related tests in the package pass;ruffclean.Server side: AgentHubService#2337 (merged).
Jira: PRODEV-2011
🤖 Generated with Claude Code